package com.rxh.utils;

import com.rxh.pojo.Excel;
import com.rxh.pojo.core.CoreOrderChange;
import org.apache.commons.csv.CSVRecord;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: 陈俊雄
 * Date: 2018/4/2
 * Time: 14:37
 * Project: Management
 * Package: com.rxh.utils
 */
public class BatchOperationUtils {

    private final static int FIRST_SHEET = 0;
    private final static int FIRST_ROW = 0;

    private static Integer indexOrder = null;
    private static Integer indexAmount = null;
    private static Integer indexReasons = null;
    private static Integer indexRemark = null;
    private static List<CSVRecord> records;

    public static List<CoreOrderChange> getOperation(Excel excel, String userName) {
        String[] fileName = excel.getFileName().split("\\.");
        switch (fileName[0]) {
            case "批量冻结":
                return null;
            case "批量退款":
                excel.setType("批量退款");
                return setChangeInfo(excel, userName, SystemConstant.EXCEPTION_STATUS_REFUND);
            case "批量拒付":
                excel.setType("批量拒付");
                return setChangeInfo(excel, userName, SystemConstant.EXCEPTION_STATUS_REFUSED);
            default:
                return null;
        }
    }

    private static void getIndex(Excel excel) {
        if (excel.getWorkbook() != null) {
            for (Cell cell : excel.getWorkbook().getSheetAt(FIRST_SHEET).getRow(FIRST_ROW)) {
                switch (cell.getStringCellValue()) {
                    case "平台订单号":
                        indexOrder = cell.getColumnIndex();
                        break;
                    case "退款金额":
                        indexAmount = cell.getColumnIndex();
                        break;
                    case "理由":
                        indexReasons = cell.getColumnIndex();
                        break;
                    case "备注":
                        indexRemark = cell.getColumnIndex();
                        break;
                    default:
                        break;
                }
            }
        } else if (excel.getCsvParser() != null) {
            try {
                records = excel.getCsvParser().getRecords();
                CSVRecord record = records.get(0);
                for (int i = 0; i < record.size(); i++) {
                    switch (record.get(i)) {
                        case "平台订单号":
                            indexOrder = i;
                            break;
                        case "退款金额":
                            indexAmount = i;
                            break;
                        case "理由":
                            indexReasons = i;
                            break;
                        case "备注":
                            indexRemark = i;
                            break;
                        default:
                            break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static List<CoreOrderChange> setChangeInfo(Excel excel, String userName, Short type) {
        List<CoreOrderChange> orderChanges = new ArrayList<>();
        getIndex(excel);
        Pattern pattern = Pattern.compile("\\s*|\t|\r|\n");
        try {
            if (indexOrder != null && indexReasons != null && indexRemark != null && type == 4) {
                if (excel.getWorkbook() != null) {
                    for (int i = 1; i <= excel.getWorkbook().getSheetAt(FIRST_SHEET).getLastRowNum(); i++) {
                        CoreOrderChange orderChange = new CoreOrderChange();
                        orderChange.setOrderId(Long.valueOf(excel.getWorkbook().getSheetAt(FIRST_SHEET).getRow(i).getCell(indexOrder).getStringCellValue()));
                        orderChange.setReasons(excel.getWorkbook().getSheetAt(FIRST_SHEET).getRow(i).getCell(indexReasons).getStringCellValue());
                        orderChange.setRemark(excel.getWorkbook().getSheetAt(FIRST_SHEET).getRow(i).getCell(indexRemark, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue());
                        orderChange.setApplicant(userName);
                        orderChange.setType(type);
                        orderChanges.add(orderChange);
                    }
                    return orderChanges;
                } else if (excel.getCsvParser() != null) {
                    for (int i = 1; i < records.size(); i++) {
                        CoreOrderChange orderChange = new CoreOrderChange();
                        orderChange.setOrderId(Long.valueOf(records.get(i).get(indexOrder)));
                        orderChange.setReasons(records.get(i).get(indexReasons));
                        orderChange.setRemark(records.get(i).get(indexRemark));
                        orderChange.setApplicant(userName);
                        orderChange.setType(type);
                        orderChanges.add(orderChange);
                    }
                    return orderChanges;
                }
            } else if (indexOrder != null && indexAmount != null && indexReasons != null && indexRemark != null && type == 2) {
                if (excel.getWorkbook() != null) {
                    CoreOrderChange orderChange = new CoreOrderChange();
                    for (int i = 1; i < excel.getCsvParser().getRecords().size(); i++) {
                        orderChange.setOrderId(Long.valueOf(excel.getWorkbook().getSheetAt(FIRST_SHEET).getRow(i).getCell(indexOrder).getStringCellValue()));
                        orderChange.setReasons(excel.getWorkbook().getSheetAt(FIRST_SHEET).getRow(i).getCell(indexReasons).getStringCellValue());
                        Matcher matcher = pattern.matcher(excel.getWorkbook().getSheetAt(FIRST_SHEET).getRow(i).getCell(indexAmount).getStringCellValue());
                        orderChange.setAmount(new BigDecimal(matcher.replaceAll("")));
                        orderChange.setRemark(excel.getWorkbook().getSheetAt(FIRST_SHEET).getRow(i).getCell(indexRemark, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue());
                        orderChange.setApplicant(userName);
                        orderChange.setType(type);
                        orderChanges.add(orderChange);
                    }
                    return orderChanges;
                } else if (excel.getCsvParser() != null) {
                    for (int i = 1; i < records.size(); i++) {
                        CoreOrderChange orderChange = new CoreOrderChange();
                        orderChange.setOrderId(Long.valueOf(records.get(i).get(indexOrder)));
                        orderChange.setReasons(records.get(i).get(indexReasons));
                        Matcher matcher = pattern.matcher(records.get(i).get(indexAmount));
                        orderChange.setAmount(new BigDecimal(matcher.replaceAll("")));
                        orderChange.setRemark(records.get(i).get(indexRemark));
                        orderChange.setApplicant(userName);
                        orderChange.setType(type);
                        orderChanges.add(orderChange);
                    }
                    return orderChanges;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}